<?php


abstract class BaseKernelDocumentoAccion extends BaseObject  implements Persistent {


	
	protected static $peer;

	
	protected $id;

	
	protected $accion_id;

	
	protected $documento_id;

	
	protected $aKernelAccion;

	
	protected $aKernelDocumento;

	
	protected $collKernelPerfilPlantillas;

	
	private $lastKernelPerfilPlantillaCriteria = null;

	
	protected $collKernelPerfilUsuarios;

	
	private $lastKernelPerfilUsuarioCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function __construct()
	{
		$this->applyDefaultValues();
	}

	
	public function applyDefaultValues()
	{
	}

	
	public function getId()
	{
		return $this->id;
	}

	
	public function getAccionId()
	{
		return $this->accion_id;
	}

	
	public function getDocumentoId()
	{
		return $this->documento_id;
	}

	
	public function setId($v)
	{
		if ($v !== null) {
			$v = (int) $v;
		}

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = KernelDocumentoAccionPeer::ID;
		}

		return $this;
	} 
	
	public function setAccionId($v)
	{
		if ($v !== null) {
			$v = (int) $v;
		}

		if ($this->accion_id !== $v) {
			$this->accion_id = $v;
			$this->modifiedColumns[] = KernelDocumentoAccionPeer::ACCION_ID;
		}

		if ($this->aKernelAccion !== null && $this->aKernelAccion->getId() !== $v) {
			$this->aKernelAccion = null;
		}

		return $this;
	} 
	
	public function setDocumentoId($v)
	{
		if ($v !== null) {
			$v = (int) $v;
		}

		if ($this->documento_id !== $v) {
			$this->documento_id = $v;
			$this->modifiedColumns[] = KernelDocumentoAccionPeer::DOCUMENTO_ID;
		}

		if ($this->aKernelDocumento !== null && $this->aKernelDocumento->getId() !== $v) {
			$this->aKernelDocumento = null;
		}

		return $this;
	} 
	
	public function hasOnlyDefaultValues()
	{
						if (array_diff($this->modifiedColumns, array())) {
				return false;
			}

				return true;
	} 
	
	public function hydrate($row, $startcol = 0, $rehydrate = false)
	{
		try {

			$this->id = ($row[$startcol + 0] !== null) ? (int) $row[$startcol + 0] : null;
			$this->accion_id = ($row[$startcol + 1] !== null) ? (int) $row[$startcol + 1] : null;
			$this->documento_id = ($row[$startcol + 2] !== null) ? (int) $row[$startcol + 2] : null;
			$this->resetModified();

			$this->setNew(false);

			if ($rehydrate) {
				$this->ensureConsistency();
			}

						return $startcol + 3; 
		} catch (Exception $e) {
			throw new PropelException("Error populating KernelDocumentoAccion object", $e);
		}
	}

	
	public function ensureConsistency()
	{

		if ($this->aKernelAccion !== null && $this->accion_id !== $this->aKernelAccion->getId()) {
			$this->aKernelAccion = null;
		}
	
		if ($this->aKernelDocumento !== null && $this->documento_id !== $this->aKernelDocumento->getId()) {
			$this->aKernelDocumento = null;
		}
	
	} 
	
	public function reload($deep = false, PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("Cannot reload a deleted object.");
		}

		if ($this->isNew()) {
			throw new PropelException("Cannot reload an unsaved object.");
		}

		if ($con === null) {
			$con = Propel::getConnection(KernelDocumentoAccionPeer::DATABASE_NAME, Propel::CONNECTION_READ);
		}

				
		$stmt = KernelDocumentoAccionPeer::doSelectStmt($this->buildPkeyCriteria(), $con);
		$row = $stmt->fetch(PDO::FETCH_NUM);
		$stmt->closeCursor();
		if (!$row) {
			throw new PropelException('Cannot find matching row in the database to reload object values.');
		}
		$this->hydrate($row, 0, true); 
		if ($deep) {  
			$this->aKernelAccion = null;
			$this->aKernelDocumento = null;
			$this->collKernelPerfilPlantillas = null;
			$this->lastKernelPerfilPlantillaCriteria = null;

			$this->collKernelPerfilUsuarios = null;
			$this->lastKernelPerfilUsuarioCriteria = null;

		} 	}

	
	public function delete(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(KernelDocumentoAccionPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}

		try {
			$con->beginTransaction();
			KernelDocumentoAccionPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	public function save(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(KernelDocumentoAccionPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}

		try {
		    if($this->validate())
		    {
    			$con->beginTransaction();
    			$affectedRows = $this->doSave($con);
    			$con->commit();
    			KernelDocumentoAccionPeer::addInstanceToPool($this);
    			return true;
			}else
			{
			     
                 return false;
            }
		} catch (PropelException $e) {
			$con->rollback();
			
			
			ErrorLog::reportarError('errors', 'error',  $e->getMessage() );
			return false;
		}
	}

	
	protected function doSave(PropelPDO $con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;

												
			if ($this->aKernelAccion !== null) {
				if ($this->aKernelAccion->isModified() || $this->aKernelAccion->isNew()) {
					$affectedRows += $this->aKernelAccion->save($con);
				}
				$this->setKernelAccion($this->aKernelAccion);
			}

			if ($this->aKernelDocumento !== null) {
				if ($this->aKernelDocumento->isModified() || $this->aKernelDocumento->isNew()) {
					$affectedRows += $this->aKernelDocumento->save($con);
				}
				$this->setKernelDocumento($this->aKernelDocumento);
			}


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = KernelDocumentoAccionPeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setId($pk);  
					$this->setNew(false);
				} else {
					$affectedRows += KernelDocumentoAccionPeer::doUpdate($this, $con);
				}

				$this->resetModified(); 			}

			if ($this->collKernelPerfilPlantillas !== null) {
				foreach ($this->collKernelPerfilPlantillas as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			if ($this->collKernelPerfilUsuarios !== null) {
				foreach ($this->collKernelPerfilUsuarios as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;

		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


												
			if ($this->aKernelAccion !== null) {
				if (!$this->aKernelAccion->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aKernelAccion->getValidationFailures());
				}
			}

			if ($this->aKernelDocumento !== null) {
				if (!$this->aKernelDocumento->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aKernelDocumento->getValidationFailures());
				}
			}


			if (($retval = KernelDocumentoAccionPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collKernelPerfilPlantillas !== null) {
					foreach ($this->collKernelPerfilPlantillas as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}

				if ($this->collKernelPerfilUsuarios !== null) {
					foreach ($this->collKernelPerfilUsuarios as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}


			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = KernelDocumentoAccionPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->getByPosition($pos);
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getAccionId();
				break;
			case 2:
				return $this->getDocumentoId();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true)
	{
		$keys = KernelDocumentoAccionPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getAccionId(),
			$keys[2] => $this->getDocumentoId(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = KernelDocumentoAccionPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setAccionId($value);
				break;
			case 2:
				$this->setDocumentoId($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = KernelDocumentoAccionPeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setAccionId($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setDocumentoId($arr[$keys[2]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(KernelDocumentoAccionPeer::DATABASE_NAME);

		if ($this->isColumnModified(KernelDocumentoAccionPeer::ID)) $criteria->add(KernelDocumentoAccionPeer::ID, $this->id);
		if ($this->isColumnModified(KernelDocumentoAccionPeer::ACCION_ID)) $criteria->add(KernelDocumentoAccionPeer::ACCION_ID, $this->accion_id);
		if ($this->isColumnModified(KernelDocumentoAccionPeer::DOCUMENTO_ID)) $criteria->add(KernelDocumentoAccionPeer::DOCUMENTO_ID, $this->documento_id);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(KernelDocumentoAccionPeer::DATABASE_NAME);

		$criteria->add(KernelDocumentoAccionPeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setAccionId($this->accion_id);

		$copyObj->setDocumentoId($this->documento_id);


		if ($deepCopy) {
									$copyObj->setNew(false);

			foreach ($this->getKernelPerfilPlantillas() as $relObj) {
				if ($relObj !== $this) {  					$copyObj->addKernelPerfilPlantilla($relObj->copy($deepCopy));
				}
			}

			foreach ($this->getKernelPerfilUsuarios() as $relObj) {
				if ($relObj !== $this) {  					$copyObj->addKernelPerfilUsuario($relObj->copy($deepCopy));
				}
			}

		} 

		$copyObj->setNew(true);

		$copyObj->setId(NULL); 
	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new KernelDocumentoAccionPeer();
		}
		return self::$peer;
	}

	
	public function setKernelAccion(KernelAccion $v = null)
	{
		if ($v === null) {
			$this->setAccionId(NULL);
		} else {
			$this->setAccionId($v->getId());
		}

		$this->aKernelAccion = $v;

						if ($v !== null) {
			$v->addKernelDocumentoAccion($this);
		}

		return $this;
	}


	
	public function getKernelAccion(PropelPDO $con = null)
	{
		if ($this->aKernelAccion === null && ($this->accion_id !== null)) {
			$this->aKernelAccion = KernelAccionPeer::retrieveByPK($this->accion_id, $con);
			
		}
		return $this->aKernelAccion;
	}

	
	public function setKernelDocumento(KernelDocumento $v = null)
	{
		if ($v === null) {
			$this->setDocumentoId(NULL);
		} else {
			$this->setDocumentoId($v->getId());
		}

		$this->aKernelDocumento = $v;

						if ($v !== null) {
			$v->addKernelDocumentoAccion($this);
		}

		return $this;
	}


	
	public function getKernelDocumento(PropelPDO $con = null)
	{
		if ($this->aKernelDocumento === null && ($this->documento_id !== null)) {
			$this->aKernelDocumento = KernelDocumentoPeer::retrieveByPK($this->documento_id, $con);
			
		}
		return $this->aKernelDocumento;
	}

	
	public function clearKernelPerfilPlantillas()
	{
		$this->collKernelPerfilPlantillas = null; 	}

	
	public function getKernelPerfilPlantillas($criteria = null, PropelPDO $con = null)
	{
		
		if ($criteria === null) {
			$criteria = new Criteria(KernelDocumentoAccionPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collKernelPerfilPlantillas === null) {
			if ($this->isNew()) {
			   $this->collKernelPerfilPlantillas = array();
			} else {

				$criteria->add(KernelPerfilPlantillaPeer::DOCUMENTO_ACCION_ID, $this->getId());

				KernelPerfilPlantillaPeer::addSelectColumns($criteria);
				$this->collKernelPerfilPlantillas = KernelPerfilPlantillaPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(KernelPerfilPlantillaPeer::DOCUMENTO_ACCION_ID, $this->getId());

				KernelPerfilPlantillaPeer::addSelectColumns($criteria);
				if (!isset($this->lastKernelPerfilPlantillaCriteria) || !$this->lastKernelPerfilPlantillaCriteria->equals($criteria)) {
					$this->collKernelPerfilPlantillas = KernelPerfilPlantillaPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastKernelPerfilPlantillaCriteria = $criteria;
		return $this->collKernelPerfilPlantillas;
	}

	
	public function countKernelPerfilPlantillas(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
	{
		
		if ($criteria === null) {
			$criteria = new Criteria(KernelDocumentoAccionPeer::DATABASE_NAME);
		} else {
			$criteria = clone $criteria;
		}

		if ($distinct) {
			$criteria->setDistinct();
		}

		$count = null;

		if ($this->collKernelPerfilPlantillas === null) {
			if ($this->isNew()) {
				$count = 0;
			} else {

				$criteria->add(KernelPerfilPlantillaPeer::DOCUMENTO_ACCION_ID, $this->getId());

				$count = KernelPerfilPlantillaPeer::doCount($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(KernelPerfilPlantillaPeer::DOCUMENTO_ACCION_ID, $this->getId());

				if (!isset($this->lastKernelPerfilPlantillaCriteria) || !$this->lastKernelPerfilPlantillaCriteria->equals($criteria)) {
					$count = KernelPerfilPlantillaPeer::doCount($criteria, $con);
				} else {
					$count = count($this->collKernelPerfilPlantillas);
				}
			} else {
				$count = count($this->collKernelPerfilPlantillas);
			}
		}
		$this->lastKernelPerfilPlantillaCriteria = $criteria;
		return $count;
	}

	
	public function addKernelPerfilPlantilla(KernelPerfilPlantilla $l)
	{
		if ($this->collKernelPerfilPlantillas === null) {
			$this->collKernelPerfilPlantillas = array();
		}
		if (!in_array($l, $this->collKernelPerfilPlantillas, true)) { 			array_push($this->collKernelPerfilPlantillas, $l);
			$l->setKernelDocumentoAccion($this);
		}
	}


	
	public function getKernelPerfilPlantillasJoinKernelPlantilla($criteria = null, $con = null)
	{
		
		if ($criteria === null) {
			$criteria = new Criteria(KernelDocumentoAccionPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collKernelPerfilPlantillas === null) {
			if ($this->isNew()) {
				$this->collKernelPerfilPlantillas = array();
			} else {

				$criteria->add(KernelPerfilPlantillaPeer::DOCUMENTO_ACCION_ID, $this->getId());

				$this->collKernelPerfilPlantillas = KernelPerfilPlantillaPeer::doSelectJoinKernelPlantilla($criteria, $con);
			}
		} else {
									
			$criteria->add(KernelPerfilPlantillaPeer::DOCUMENTO_ACCION_ID, $this->getId());

			if (!isset($this->lastKernelPerfilPlantillaCriteria) || !$this->lastKernelPerfilPlantillaCriteria->equals($criteria)) {
				$this->collKernelPerfilPlantillas = KernelPerfilPlantillaPeer::doSelectJoinKernelPlantilla($criteria, $con);
			}
		}
		$this->lastKernelPerfilPlantillaCriteria = $criteria;

		return $this->collKernelPerfilPlantillas;
	}

	
	public function clearKernelPerfilUsuarios()
	{
		$this->collKernelPerfilUsuarios = null; 	}

	
	public function getKernelPerfilUsuarios($criteria = null, PropelPDO $con = null)
	{
		
		if ($criteria === null) {
			$criteria = new Criteria(KernelDocumentoAccionPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collKernelPerfilUsuarios === null) {
			if ($this->isNew()) {
			   $this->collKernelPerfilUsuarios = array();
			} else {

				$criteria->add(KernelPerfilUsuarioPeer::DOCUMENTO_ACCION_ID, $this->getId());

				KernelPerfilUsuarioPeer::addSelectColumns($criteria);
				$this->collKernelPerfilUsuarios = KernelPerfilUsuarioPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(KernelPerfilUsuarioPeer::DOCUMENTO_ACCION_ID, $this->getId());

				KernelPerfilUsuarioPeer::addSelectColumns($criteria);
				if (!isset($this->lastKernelPerfilUsuarioCriteria) || !$this->lastKernelPerfilUsuarioCriteria->equals($criteria)) {
					$this->collKernelPerfilUsuarios = KernelPerfilUsuarioPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastKernelPerfilUsuarioCriteria = $criteria;
		return $this->collKernelPerfilUsuarios;
	}

	
	public function countKernelPerfilUsuarios(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
	{
		
		if ($criteria === null) {
			$criteria = new Criteria(KernelDocumentoAccionPeer::DATABASE_NAME);
		} else {
			$criteria = clone $criteria;
		}

		if ($distinct) {
			$criteria->setDistinct();
		}

		$count = null;

		if ($this->collKernelPerfilUsuarios === null) {
			if ($this->isNew()) {
				$count = 0;
			} else {

				$criteria->add(KernelPerfilUsuarioPeer::DOCUMENTO_ACCION_ID, $this->getId());

				$count = KernelPerfilUsuarioPeer::doCount($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(KernelPerfilUsuarioPeer::DOCUMENTO_ACCION_ID, $this->getId());

				if (!isset($this->lastKernelPerfilUsuarioCriteria) || !$this->lastKernelPerfilUsuarioCriteria->equals($criteria)) {
					$count = KernelPerfilUsuarioPeer::doCount($criteria, $con);
				} else {
					$count = count($this->collKernelPerfilUsuarios);
				}
			} else {
				$count = count($this->collKernelPerfilUsuarios);
			}
		}
		$this->lastKernelPerfilUsuarioCriteria = $criteria;
		return $count;
	}

	
	public function addKernelPerfilUsuario(KernelPerfilUsuario $l)
	{
		if ($this->collKernelPerfilUsuarios === null) {
			$this->collKernelPerfilUsuarios = array();
		}
		if (!in_array($l, $this->collKernelPerfilUsuarios, true)) { 			array_push($this->collKernelPerfilUsuarios, $l);
			$l->setKernelDocumentoAccion($this);
		}
	}

	
	public function clearAllReferences($deep = false)
	{
		if ($deep) {
			foreach($this->collKernelPerfilPlantillas as $o) {
				$o->clearAllReferences($deep);
			}
			foreach($this->collKernelPerfilUsuarios as $o) {
				$o->clearAllReferences($deep);
			}
		} 
		$this->collKernelPerfilPlantillas = null;
		$this->collKernelPerfilUsuarios = null;
	}

} 